/*
	TSortList
*/
template<class T>
class TSortList : public TList<T>
{
public:
	class ISortComparer;
private:
	ref<ISortComparer> mSortComparer;
public:
	inline TSortList(ref<ISortComparer> enm)
	{
		mSortComparer = enm;
	}

	inline T Add(T& inst)
	{
		if(Length() == 0)
		{
			return TList::Add(inst);
		}
		else
		{
			for(Iterator it = Begin(), end = End(); it!=end; ++it)
			{
				if(mSortComparer->Sort(it->GetObject(), inst))
				{
					return Insert(*it, inst);
				}
			}
		}
		//throw error
		return NullPtr;
	}
};

/*
	SortList
*/
template<class T>
class SortList : public ref<TSortList<T> >
{
public:
	typedef typename TSortList<T>::ListItem ListItem;
	typedef typename TSortList<T>::Iterator Iterator; 
public:
	inline SortList() : ref() {}
        inline SortList(ref<TSortList<T> > copy) : ref(copy) {} // for Cast
};


/*
	ISortComparer
*/

template<class T>
class TSortList<T>::ISortComparer : public TObject
{
public:
	virtual bool Sort(T item1, T item2) = 0;
};
